package cn.yun.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/*
演示Request对象获取请求头数据
 Enumeration<>  getHeaderNames():获取请求头的所有名称 返回Enumeration对象
   String getHeader(String name):获取请求头中指定名称的数据

Enumeration:类似于迭代器
 方法: hasMoreElements():测试此枚举是否包含更多元素
          nextElement():如果此枚举对象至少有一个要提供的元素,返回此枚举的下一个元素

*/
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //1.获取请求头中所有的名称 getHeaderNames():
        Enumeration<String> headerNames = request.getHeaderNames();
        //2.遍历Enumeration,
       while(headerNames.hasMoreElements() ){
           String name = headerNames.nextElement();
           //根据名称获取请求头中每一个名称对应的数据
           String value = request.getHeader(name);
           System.out.println(name+":"+value);
       }

    }
}
